
package com.probiz.estore.system.service.impl;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.system.AppAudit;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.system.dao.AppAuditDao;
import com.probiz.estore.system.service.AppAuditManager;
import com.probiz.estore.util.ipwry.IPSeeker;

/**
 * AppAudit Business Delegate (Proxy) implementation to handle communication
 * between web and persistence layer. Implementation of AppAuditManager
 * interface. Developer introduced interfaces should be declared here. Won't get
 * overwritten.
 */
public class AppAuditManagerImpl extends GenericManagerImpl<AppAudit> implements AppAuditManager {

	private AppAuditDao appAuditDao = null;
	private final static String LOCAL_ADDR = "0:0:0:0:0:0:0:1";
	public void setAppAuditDao(AppAuditDao appAuditDao) {
		this.appAuditDao = appAuditDao;
	}

	protected void doAuditAction(AppAudit appAudit) {
		save(appAudit);
	}

	public void doAuditAction(String actionName, String procObj, String procResult, String remoteIp, String procUsername) {
		AppAudit audit = new AppAudit();
		audit.setActionName(actionName);
		audit.setProcObj(procObj);
		audit.setProcResult(procResult);
		audit.setProcUsername(procUsername);
		if(StringUtils.isNotBlank(remoteIp)){
			if(LOCAL_ADDR.equals(remoteIp.trim())){
				audit.setRemoteIpInfo(I18nUtil.getInstance().getMessage("appAudit.localAddr"));
			}else{
				String address = IPSeeker.getInstance().getAddress(remoteIp);
				audit.setRemoteIpInfo(remoteIp+" "+address);
			}
		} 
		audit.setProcTime(new Date());
		save(audit);
	}
	
	@Override
	protected void initManager() {
		dao = appAuditDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(AppAudit entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(AppAudit entity) {

	}

}
